package info.macnana.online.jwt.filter;

import info.macnana.online.jwt.config.JwtConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.web.authentication.preauth.RequestHeaderAuthenticationFilter;
import org.springframework.stereotype.Component;

/**
 * author: zhengheng
 * github: https://github.com/macnana777
 * email: 517862340@qq.com
 * <p>
 * Date: 2017-08-10 10:56
 * Description:定制授权过滤器，将能读取请求头部信息，
 * 在Spring中已经有一个这样的授权Filter称为：RequestHeaderAuthenticationFilter，
 * 我们只要扩展继承即可
 * Copyright(©) 2017 by zhengheng.
 */
@Component
public class JWTAuthenticationFilter extends RequestHeaderAuthenticationFilter {

    @Autowired
    public JWTAuthenticationFilter(JwtConfig jwtConfig) {
        // Don't throw exceptions if the header is missing
        this.setExceptionIfHeaderMissing(false);
        // This is the request header it will look for
        this.setPrincipalRequestHeader(jwtConfig.getTokenHeaderKey());
    }

    @Override
    @Autowired
    public void setAuthenticationManager(
            AuthenticationManager authenticationManager) {
        //在这里，头部信息将被转换为Spring Authentication对象，
        // 名称为PreAuthenticatedAuthenticationToken
        super.setAuthenticationManager(authenticationManager);
    }



}
